<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Home\Controller;

use Think\Controller;

/**
 * 林州市悟能科技提供技术支持
 * 400-037203
 * @author Mr_Lee :672288511;
 */
class WxController extends Controller {
 public function _initialize(){
       if(empty($_SESSION['username'])){
              
 //$this->error('页面加载中。。。',U('Login/Login'),1);
    $this->redirect('Login/Login');

       }
    $table1="wx";
        $this->assign('table',$table1);
    }
 public function wx_menu()
    {$menu=M('wxch_menu');
     $map['wid']=I('get.wid');
         if(IS_POST){
          $menu->create();
          $up=$menu->where($map)->save();
          if($up){
              
           $this->success('自定义菜单保存成功');   
          }else{
             $this->error('自定义菜单保存失败'); 
              
          }
 }else{
     
    
     $data=$menu->where($map)->find();
     $this->assign('data',$data);
  
     $this->display();
 }      
 
    }
    //生成自定义菜单
    public function create_menu(){
        
        $menu=M('wxch_menu');
        $map['wid']=I('get.wid');
        $sj=$menu->where($map)->find();
  
        
        for($i=1;$i<=5;$i++){
            if($sj['first1_'.$i.'_type']=="view")
            {$s='url';
            
            }   elseif($sj['first1_'.$i.'_type']=="click"){
                $s='key';
            }  
             if(!empty($sj['first1_'.$i.''])){
         $arr1[]= array('type'=>$sj['first1_'.$i.'_type'],'name'=>urlencode($sj['first1_'.$i.'']),$s=>urlencode($sj['first1_'.$i.'_value']));   
             }
        }
 for($i=1;$i<=5;$i++){
            if($sj['second1_'.$i.'_type']=="view")
            {$s='url';
            
            }   elseif($sj['second1_'.$i.'_type']=="click"){
                $s='key';
            }  
              if(!empty($sj['second1_'.$i.''])){
         $arr2[]= array('type'=>$sj['second1_'.$i.'_type'],'name'=>urlencode($sj['second1_'.$i.'']),$s=>urlencode($sj['second1_'.$i.'_value']));   
              }
        }
  for($i=1;$i<=5;$i++){
            if($sj['third1_'.$i.'_type']=="view")
            {$s='url';
            
            }   elseif($sj['third1_'.$i.'_type']=="click"){
                $s='key';
            }  
            if(!empty($sj['third1_'.$i.''])){
                $arr3[]= array('type'=>$sj['third1_'.$i.'_type'],'name'=>urlencode($sj['third1_'.$i.'']),$s=>urlencode($sj['third1_'.$i.'_value']));  
                
            }
          
            
        }       
        if(!empty($sj['first'])){
            if($sj['first_type']=="view")
            {$s='url';
            
            }   elseif($sj['first_type']=="click"){
                $s='key';
            }  
         if(empty($sj['first_value'])){
  
    		$sarr1 = array('name'=>urlencode($sj['first']),'sub_button'=>$arr1);
        }
    	else
    	{
    		$sarr1 = array('type'=>$sj['first_type'],'name'=>urlencode($sj['first']),$s=>urlencode($sj['first_value']));  
        }  }  else{
            unset($sarr1);
            
        } 
        if(!empty($sj['second'])){
                    if($sj['second_type']=="view")
            {$s='url';
            
            }   elseif($sj['second_type']=="click"){
                $s='key';
            }  
          if(empty($sj['second_value'])){
  
    		$sarr2 = array('name'=>urlencode($sj['second']),'sub_button'=>$arr2);
        }
    	else
    	{
    		$sarr2 = array('type'=>$sj['second_type'],'name'=>urlencode($sj['second']),$s=>urlencode($sj['second_value']));  
        }  } else{
            unset($sarr2);
            
        } 
        if(!empty($sj['third'])){
                    if($sj['third_type']=="view")
            {$s='url';
            
            }   elseif($sj['third_type']=="click"){
                $s='key';
            }  
          if(empty($sj['third_value'])){
  
    		$sarr3 = array('name'=>urlencode($sj['third']),'sub_button'=>$arr3);
        }
    	else
    	{
    		$sarr3 = array('type'=>$sj['third_type'],'name'=>urlencode($sj['third']),$s=>urlencode($sj['third_value']));  
    }} else{
            unset($sarr3);
            
        } 
        $aarr=array($sarr1,$sarr2,$sarr3);
         $darr=array_filter($aarr);
$arr = array( 'button' =>$darr );

    	$menu = urldecode(json_encode($arr,true));
    
       $this->tj_menu($menu);
    }

    public function tj_menu($menu){
       import('Common.Wechat.Wechat');
                    $map['id']=1;
                    $config=M('User')->where($map)->find();
                    $options = array (
				'token' => "weilinzhou", // 填写你设定的key
				//'encodingaeskey' => $config ["encodingaeskey"], // 填写加密用的EncodingAESKey
				'appid' => $config['appid'], // 填写高级调用功能的app id
				'appsecret' => $config['appsecret'], // 填写高级调用功能的密钥
				
				//'paysignkey' => $config ["paysignkey"]  // 商户签名密钥Key
				);
		$weObj = new \Common\Wechat\Wechat ( $options );  
		$s=$weObj ->createMenu($menu);
if($s){
    
  $this->success('生成自定义菜单成功');  
}else{
    $this->error('生成自定义菜单失败');
    
}
    
}
public function huifu(){
    
 if(IS_POST){
    
     if(!empty($_FILES['picurl']['name'])){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件 
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功
        $data['picurl']="http://".$_SERVER['SERVER_NAME']."/Uploads/".$info['picurl']['savepath'].$info['picurl']['savename'];
        

        }
        
    }else{
        
     $data['picurl']=I('post.picurl2');
    }
    //    echo 12;die;
        $data['title']=I('post.title');
        $data['key']=I('post.key');
        $data['description']=I('post.description');
        $data['url']=I('post.url');
        $data['info']=I('post.info');
        $data['id']=I('post.id');
        // dump($data);die;
       $add=M('gzhf')->save($data);
       if($add){
           
           $this->success('更改成功');
       }else{
           
           $this->error('更改失败');
       }
    
     
     
 }else{
     
   $info=M('gzhf')->find();;  
     $this->assign('info',$info);
     $this->display();
 }
}
public function zdhf(){
   $zdhf=M('zdhf')->select();
    $this->assign('zdhf',$zdhf);

    $this->display();

    //put your code here
}
public function zdhfadd(){
   if(IS_AJAX){
    $data['keyword']=I('post.keyword');
    $data['info']=I('post.info');
    $add=M('zdhf')->add($data);
    if($add){
        $datas=0;
    }else{
        $datas=1;
    }
    echo $datas;
   
   }else{
    $this->display();
   }
}
public function zdhfxg(){
   if(IS_AJAX){
    $data['keyword']=I('post.keyword');
    $data['info']=I('post.info');
    $add=M('zdhf')->where(array('id'=>I('post.id')))->save($data);
    if($add){
        $datas=0;
    }else{
        $datas=1;
    }
    echo $datas;
   
   }else{
     $zdhf=M('zdhf')->where(array('id'=>I('get.id')))->find();
    $this->assign('zdhf',$zdhf);
    $this->display();
   }
}
public function zdhfdel(){

   $map['id']=I('get.id');
   $del=M('zdhf')->where($map)->delete();
   if($del){
    $this->success('删除成功');
   }else{
    $this-error('删除失败');
   }


}
}
